package com.miku.sys.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.druid.util.Base64;
import com.miku.common.BaseController;
import com.miku.sys.utils.ImageUtils;
import com.miku.sys.utils.ImageUtils.Operator;
import com.miku.utils.IOUtils;
import com.miku.utils.RespUtils;

/**
 * 这个controller主要负责提取图片的问题，由于图片会有多种尺度
 * 主要在这里被处理
 * @author 杜金伟 [2016年3月2日 下午2:42:30]
 */
@Controller
@RequestMapping("/img")
public class ImageController extends BaseController {
	
	//首先，图片分为几个种类
	//1，产品图片
	//2，品牌图片
	//3，用户头像
	//4，排行榜宣传图片
	
	
	//图片定义：
	//用户图片：url=user/123/0    表示123用户的第0套图
	//总共2套图目前：
	//0：第0套，尺寸是：58*58
	
	//第0套图的尺寸，用于社区等小图显示的位置
	//第1套图的尺寸，用于个人中心大图显示的位置
	private static final int[] userWidth = {32, 58, 190, 162 ,72, 115}; 
	private static final int[] userHeight = {32, 58, 190, 162 ,72, 115};
	
	//第0套图的尺寸，用于社区等小图显示的位置
	//第1套图的尺寸，用于个人中心大图显示的位置
	private static final int[] postsWidth = {ImageUtils.sizeTemplateRaw, 300}; 
	private static final int[] postsHeight = {ImageUtils.sizeTemplateRaw, 300};
	
	@RequestMapping("/user/{idd}/{n}")
	public void user(@PathVariable("idd") int idd, @PathVariable("n") int n, HttpServletRequest request, HttpServletResponse resp){
		//respSysImage(userWidth, userHeight, "user", idd, n, request, resp);
		if(n < 0 || n >= userWidth.length) {
			resp.setStatus(HttpStatus.SC_NOT_FOUND);
			return;
		}
		ImageUtils.respImage(userWidth[n], userHeight[n], "/sys/user", idd+"", Operator.Resize, request, resp);
	}
	
	@RequestMapping("/user/{idd}/{width}x{height}/g")
	public void userX(@PathVariable("idd") int idd, @PathVariable("width") int w, @PathVariable("height") int h, 
			HttpServletRequest request, HttpServletResponse resp){
		//respSysImage(userWidth, userHeight, "user", idd, n, request, resp);
		ImageUtils.respImage(w, h, "/sys/user", idd+"", Operator.Resize, request, resp);
	}
	
	@ResponseBody
	@RequestMapping("/upload/posts")
	public String uploadPosts(String image){
		
		//移除头上的data:image/jpeg,base64,字样，如果有
		int flag = image.indexOf("base64,");
		if(flag != -1){
			image = image.substring(flag + 7);
			image = StringEscapeUtils.unescapeHtml(image);
		}
		
		byte[] bs = Base64.base64ToByteArray(image);
		if(bs == null) return RespUtils.respFail(null);
		return RespUtils.respMsg(IOUtils.saveToFileMD5(ImageUtils.getPostsDir(), ".jpg", bs));
	}
	
	@ResponseBody
	@RequestMapping("/upload/recproduct")
	public String uploadRecproduct(String image){
		byte[] bs = Base64.base64ToByteArray(image);
		if(bs == null) return RespUtils.respFail(null);
		return RespUtils.respMsg(IOUtils.saveToFileMD5(ImageUtils.getRecproductDir(), ".jpg", bs));
	}
	
	@ResponseBody
	@RequestMapping("/upload/caseBgImg")
	public String upload(@RequestParam MultipartFile[] files, HttpServletResponse resp){
		if(files == null || files.length != 1){
			resp.setStatus(HttpStatus.SC_NOT_FOUND);
			return null;
		}
		return IOUtils.saveMultipartToFileMD5(ImageUtils.getPostsDir(), ".jpg", files[0]);
	}
	
	@RequestMapping("/posts/{md5}/{n}")
	public void postsRaw(@PathVariable("md5") String md5, @PathVariable("n") int n, HttpServletRequest request, HttpServletResponse resp){
		if(n < 0 || n >= postsWidth.length) {
			resp.setStatus(HttpStatus.SC_NOT_FOUND);
			return;
		}
		ImageUtils.respImage(postsWidth[n], postsHeight[n], "/business/posts", md5, Operator.CenterScaleCrop, request, resp);
	}
	
	@RequestMapping("/posts/{md5}")
	public void posts(@PathVariable("md5") String md5, HttpServletRequest request, HttpServletResponse resp){
		ImageUtils.respImage(postsWidth[0], postsHeight[0], "/business/posts", md5, Operator.CenterSquareCrop, request, resp);
	}
	
	/**
	 * 通用图像处理
	 */
	@RequestMapping("/{md5}/{w}x{h}")
	public void imgRaw(@PathVariable("md5") String md5, @PathVariable("w") int w, @PathVariable("h") int h, HttpServletRequest request, HttpServletResponse resp){
		//size的格式是w*h
		ImageUtils.respImage(w, h, "/generl", md5, Operator.CenterScaleCrop, request, resp);
	}
	
	@RequestMapping("/{md5}")
	public void img(@PathVariable("md5") String md5, HttpServletRequest request, HttpServletResponse resp){
		ImageUtils.respImage(ImageUtils.sizeTemplateRaw, ImageUtils.sizeTemplateRaw, "/generl", md5, Operator.CenterSquareCrop, request, resp);
	}
	
	/**
	 * 通用图像处理
	 */
	@RequestMapping("/recproduct/{md5}/{w}x{h}")
	public void recproductImageRaw(@PathVariable("md5") String md5, @PathVariable("w") int w, @PathVariable("h") int h, HttpServletRequest request, HttpServletResponse resp){
		//size的格式是w*h
		ImageUtils.respImage(w, h, "/business/recproduct", md5, Operator.CenterScaleCrop, request, resp);
	}
	
	@RequestMapping("/recproduct/{md5}")
	public void recproductImage(@PathVariable("md5") String md5, HttpServletRequest request, HttpServletResponse resp){
		ImageUtils.respImage(ImageUtils.sizeTemplateRaw, ImageUtils.sizeTemplateRaw, "/business/recproduct", md5, Operator.CenterSquareCrop, request, resp);
	}
}
